﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Form1.cs" company="Drunk in the ER Productions">
// GNU General Public License version 2 (GPLv2)
// Full text: http://robogui.codeplex.com/license  
// </copyright>
// <summary>
//   Defines the FrmRoboGui type.
// </summary>
// TODO: /Z /ZB and /B should be exclusive. Uncheck and disable others when one is selected
// --------------------------------------------------------------------------------------------------------------------

namespace RoboGUI
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    ///  Main form for RoboGUI
    /// </summary>
    public partial class FrmRoboGui : Form
    {
        /// <summary>
        /// Create a new instance of the RoboCopy class
        /// </summary>
        private RoboCopy robocopy = new RoboCopy();

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmRoboGui"/> class.
        /// </summary>
        public FrmRoboGui()
        {
            InitializeComponent();
            if (this.robocopy != null)
            {
                this.robocopy.SourceServer = this.txtSource.Text;
                this.robocopy.TargetServer = this.txtTarget.Text;
            }

            // MessageBox.Show(this.robocopy.SourceServer);
            
            // MessageBox.Show(this.robocopy.TargetServer);
        }

        /// <summary>
        /// Handles the Click event of the exitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ExitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Provide version and general program information to the user
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("RoboGUI version 0.0.0.1\nCode: Rod Stewart");
        }

        /// <summary>
        /// Provide help file or URL to the user
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HelpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To implement later");
        }

        /// <summary>
        /// Allows the user to browse to a folder / share to select the source data
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnBrowseSource_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtSource.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        /// Allows the user to browse to a folder / share to select the target data
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnBrowseTarget_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtTarget.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        /// If the user chooses chkCopyAll, automatically select all "what to copy" options
        /// and disable controls
        /// If the user deselects chkCopyAll, automatically select / deselect attributes to return to RoboCopy defaults
        /// and enable controls
        /// RoboCopy defaults to /COPY:DAT
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ChkCopyAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCopyAll.Checked) 
            {
                chkCopyData.Checked = true;
                chkCopyData.Enabled = false;
                
                chkCopyAttributes.Checked = true;
                chkCopyAttributes.Enabled = false;
                
                chkCopyTimestamps.Checked = true;
                chkCopyTimestamps.Enabled = false; 
                
                chkCopySecurity.Checked = true;
                chkCopySecurity.Enabled = false;
                
                chkCopyOwner.Checked = true;
                chkCopyOwner.Enabled = false;
                
                chkCopyAuditing.Checked = true;
                chkCopyAuditing.Enabled = false;
            }
            else if (chkCopyAll.Checked == false) 
            {
                chkCopyData.Checked = true;
                chkCopyData.Enabled = true; 

                chkCopyAttributes.Checked = true;
                chkCopyAttributes.Enabled = true; 
                
                chkCopyTimestamps.Checked = true;
                chkCopyTimestamps.Enabled = true; 
                
                chkCopySecurity.Checked = false;
                chkCopySecurity.Enabled = true;
                
                chkCopyOwner.Checked = false;
                chkCopyOwner.Enabled = true; 
                
                chkCopyAuditing.Checked = false;
                chkCopyAuditing.Enabled = true;
            }
        }

        /// <summary>
        /// If the user selects chkCopyAllSec, automatically select options for /copy:DATS
        /// and disable controls
        /// This follows RoboCopy's /SEC behavior
        /// If the user deselects chkCopyAllSec, automatically select / deselect attributes to return to RoboCopy defaults
        /// and enable controls
        /// RoboCopy defaults to /COPY:DAT
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ChkCopyAllSec_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCopyAllSec.Checked)
            {
                chkCopyData.Checked = true;
                chkCopyData.Enabled = false; 
                
                chkCopyAttributes.Checked = true;
                chkCopyAttributes.Enabled = false;
                
                chkCopyTimestamps.Checked = true;
                chkCopyTimestamps.Enabled = false;
                
                chkCopyOwner.Checked = false;
                chkCopyOwner.Enabled = false;
                
                chkCopySecurity.Checked = true;
                chkCopySecurity.Enabled = false;
                
                chkCopyAuditing.Checked = false;
                chkCopyAuditing.Enabled = false;
            }
            else if (chkCopyAllSec.Checked == false)
            {
                chkCopyData.Checked = true;
                chkCopyData.Enabled = true;

                chkCopyAttributes.Checked = true;
                chkCopyAttributes.Enabled = true;

                chkCopyTimestamps.Checked = true;
                chkCopyTimestamps.Enabled = true;
                
                chkCopyOwner.Checked = false;
                chkCopyOwner.Enabled = true;
                
                chkCopySecurity.Checked = false;
                chkCopySecurity.Enabled = true;
                
                chkCopyAuditing.Checked = false;
                chkCopyAuditing.Enabled = true;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkCopyEmptySubs control.
        /// If the user selects ChkCopyEmptySubs there is no longer a need for
        /// ChkCopySubs and it is disabled accordingly
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ChkCopyEmptySubs_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkCopyEmptySubs.Checked)
            {
                ChkCopySubs.Checked = false;
                ChkCopySubs.Enabled = false;
            }
            else if (ChkCopyEmptySubs.Checked == false)
            {
                ChkCopySubs.Enabled = true;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkCopySubs control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ChkCopySubs_CheckedChanged(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TxtSource_TextChanged(object sender, EventArgs e)
        {
            this.robocopy.SourceServer = this.txtSource.Text;
            this.TxtCommandLine.Text = this.GenerateCommandLine();
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtTarget control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TxtTarget_TextChanged(object sender, EventArgs e)
        {
            this.robocopy.TargetServer = txtTarget.Text;
            TxtCommandLine.Text = this.GenerateCommandLine();

            // MessageBox.Show(this.robocopy.TargetServer);
        }

        /// <summary>
        /// Generates the command line.
        /// </summary>
        /// <returns>string commandline</returns>
        private string GenerateCommandLine()
        {
            string commandline = "Robocopy ";

            ////commandline = "Robocopy ";

            commandline += this.robocopy.SourceServer;
            commandline += this.robocopy.TargetServer;

            if (ChkCopySubs.Checked)
            {
                commandline += this.robocopy.CopySubs;
            }

            if (ChkCopyEmptySubs.Checked)
            {
                commandline += this.robocopy.CopyEmptySubs;
            }

            if (chkRestartableMode.Checked)
            {
                commandline += this.robocopy.RestartableMode;
            }

            if (chkBackupMode.Checked)
            {
                commandline += this.robocopy.BackupMode;
            }

            if (chkBackupRestartable.Checked)
            {
                commandline += this.robocopy.RestartableBackupMode;
            }

            return commandline;
        }

        /// <summary>
        /// Handles the Click event of the BtnGenerateCommandLine control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnGenerateCommandLine_Click(object sender, EventArgs e)
        {
            TxtCommandLine.Text = this.GenerateCommandLine();
            ////TxtCommandLine.Text = "Robocopy ";
            ////TxtCommandLine.Text += this.robocopy.SourceServer;
            ////TxtCommandLine.Text += this.robocopy.TargetServer;
            
            ////if (ChkCopySubs.Checked)
            ////{
            ////    this.TxtCommandLine.Text += this.robocopy.CopySubs;   
            ////}
            ////if (ChkCopyEmptySubs.Checked)
            ////{
            ////    this.TxtCommandLine.Text += this.robocopy.CopyEmptySubs;
            ////}
            ////if (chkRestartableMode.Checked)
            ////{
            ////    this.TxtCommandLine.Text += this.robocopy.RestartableMode;
            ////}
            ////if (chkBackupMode.Checked)
            ////{
            ////    this.TxtCommandLine.Text += this.robocopy.BackupMode;
            ////}
            ////if (chkBackupRestartable.Checked)
            ////{
            ////    this.TxtCommandLine.Text += this.robocopy.RestartableBackupMode;
            ////}
        }
    }
}
